<template>
    <el-form :model="afr" :rules="rules" ref="afr" label-width="100px">
        <p>帐号注册</p>
        <el-form-item label="帐号" prop="username">
            <el-input v-model="afr.username"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
            <el-input v-model="afr.captcha"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="afr.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="afr.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('afr')">注册</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
    import axios from '~plugins/axios';

    export default {
        head() {
            return {title: '帐号注册'};
        },
        data() {
            // 密码自定义验证
            const validatePasswordCheck = (rule, value, callback) => {
                if (this.afr.confirmPassword !== '') {
                    this.$refs.afr.validateField('confirmPassword');
                }
                callback();
            };
            // 确认密码自定义验证
            const validateConfirmPasswordCheck = (rule, value, callback) => {
                value !== this.afr.password ? callback(new Error('两次输入密码不一致!')) : callback();
            };
            return {
                afr: {
                    username: '',
                    captcha: '',
                    password: '',
                    confirmPassword: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请填写用户名', trigger: 'change'}
                    ],
                    captcha: [
                        {required: true, message: '请填写验证码', trigger: 'change'}
                    ],
                    password: [
                        {required: true, message: '请填写密码', trigger: 'change'},
                        {type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'change'},
                        {type: 'string', max: 6, message: '密码长度不能大于32位', trigger: 'change'},
                        {validator: validatePasswordCheck, trigger: 'change'}
                    ],
                    confirmPassword: [
                        {required: true, message: '请填写确认密码', trigger: 'change'},
                        {validator: validateConfirmPasswordCheck, trigger: 'change'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('/register', this[name]).then(res => {
                            this.$message({message: '登录成功', type: 'success'});
                            this.$router.push('/user/info');
                        }).catch(() => {
                            this.$message({message: '登录失败', type: 'error'});
                        });
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    };
</script>